const fs = require('fs');//?文件夹操作

let server = function (app, db) {
    //?Cookie查询
    const View_Cookie = function (request, calka) {
        db.query('select * from login where UserCookie=\'' + request.cookies.DOM_Cookie + '\'', function (err, data) {
            calka(err, data)
        })
    }

    //?图片响应
    app.get('/img/:path', function (request, response) {
        fs.readFile('img/' + request.params.path, function (err, data) {
            response.send(data)
        })
    })

    //?用户头像响应
    app.get('/Head_p', function (request, response) {

        View_Cookie(request, function (err, data) {
            if (data[0]) {
                db.query('select Portrait from user where UserID=' + data[0].UserID, function (err, data1) {
                    fs.readFile('./picture/' + data1[0].Portrait, function (err, data2) {
                        if (err) {
                            console.error(err);
                            fs.readFile('./picture/default.png', function (err, data) {
                                response.send(data)
                            })
                        } else response.send(data2)
                    })
                })
            } else {
                fs.readFile('./picture/default.png', function (err, data) {
                    response.send(data)

                })
            }
        })
    })

    //?发布者头像响应
    app.get('/Head_p/:UserID', function (request, response) {
        db.query('select Portrait from user where UserID=' + request.params.UserID, function (err, data1) {
            if (data1[0]) {
                fs.readFile('./picture/' + data1[0].Portrait, function (err, data2) {
                    if (err) {
                        fs.readFile('./picture/default.png', function (err, data) {
                            response.send(data)
                        })
                    } else response.send(data2)
                })
            } else fs.readFile('./picture/default.png', function (err, data) {
                response.send(data)

            })

        })
    })

    //?样图响应
    app.get('/img/:img', function (request, response) {
        fs.readFile('picture/' + request.params.img, function (err, data) {
            response.send(data)
        })
    })

    //?图标响应
    app.get('/ico/:ico', function (request, response) {
        console.log(request.params.ico);
        fs.readFile('ico/' + request.params.ico, function (err, data) {
            response.header({"Content-Type":"image/x-icon"})
            response.send(data)
        })
    })
}
module.exports = server